package com.jycode.commons.model;
// Generated 2008-2-20 12:22:33 by Hibernate Tools 3.2.0.b9


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.jycode.commons.model.base.LabelValue;

/**
 * SysUser generated by hbm2java
 */
@SuppressWarnings("serial")
@Entity
@Table(name="SYS_USER"
    ,catalog="TEST"
)
@NamedQueries( {
	@NamedQuery(name = "SysUser.findById", query = "select u from SysUser u where id=:id"),
	@NamedQuery(name = "SysUser.findByName", query = "from SysUser u where u.email=?"),
	@NamedQuery(name = "SysUser.findByEmail", query = "from SysUser u where u.email=?"),
	@NamedQuery(name = "SysUser.findByNamePassword", query = "from SysUser u where u.email=? and u.password=?"),
	@NamedQuery(name = "SysUser.findAll", query = "select u from SysUser u"),
	@NamedQuery(name = "SysUser.delete_manual", query = "delete from SysUser where id=:id"),
	@NamedQuery(name = "SysUser.listByName", query = "from SysUser u where u.email LIKE ?"),
	@NamedQuery(name = "SysUser.checkValid", query = "select count(*) from SysUser u where u.email=? and u.password=?"),
	@NamedQuery(name = "SysUser.login", query = "from SysUser u where u.email=? and u.password=?")
})
public class SysUser /**/ implements java.io.Serializable {


     private long id;
     private String email;
     private String firstName;
     private String lastName;
     private String password;
     private Set<SysRole> sysRoles = new HashSet<SysRole>(0);

    public SysUser() {
    }


    public SysUser(long id) {
        this.id = id;
    }
    public SysUser(long id, String email, String firstName, String lastName, String password, Set<SysRole> sysRoles) {
       this.id = id;
       this.email = email;
       this.firstName = firstName;
       this.lastName = lastName;
       this.password = password;
       this.sysRoles = sysRoles;
    }

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="ID", unique=true, nullable=false)
    public long getId() {
        return this.id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Column(name="EMAIL", length=50, unique=true)
    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name="FIRST_NAME", length=30)
    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name="LAST_NAME", length=30)
    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Column(name="PASSWORD", length=30)
    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

//    @ManyToMany(cascade=CascadeType.REMOVE, fetch=FetchType.LAZY, mappedBy="sysUsers")
//    @JoinTable(
//            name="user_role",
//            joinColumns = { @JoinColumn( name="user_id") },
//            inverseJoinColumns = @JoinColumn( name="role_id")
//    )
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name="SYS_USER_ROLE_MAP",
            joinColumns = { @JoinColumn( name="USER_ID") },
            inverseJoinColumns = @JoinColumn( name="ROLE_ID")
    )
    public Set<SysRole> getSysRoles() {
        return this.sysRoles;
    }

    public void setSysRoles(Set<SysRole> sysRoles) {
        this.sysRoles = sysRoles;
    }

    /**
     * Convert user roles to LabelValue objects for convenience.
     * @return a list of LabelValue objects with role information
     */
    @Transient
    public List<LabelValue> getRoleList() {
        List<LabelValue> userRoles = new ArrayList<LabelValue>();

        if (this.sysRoles != null) {
            for (SysRole role : this.sysRoles) {
                // convert the user's roles to LabelValue Objects
                userRoles.add(new LabelValue(role.getName(), role.getName()));
            }
        }

        return userRoles;
    }




}


